package com.session05;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TestIO {
    public static void main(String[] args) {
        //输出
 /*         String msg = "人之初，性本善；";
        FileWriter fw = null;
        try {
            fw = new FileWriter("test.txt");
            fw.write(msg);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }*/

        //输入
        String msg = "";
        FileReader fr = null;
        try {
            fr = new FileReader("test.txt");
            int b = 0;
            while ((b = fr.read()) != -1) {
                msg += (char) b;
            }

            /*char[] c = new char[5];//要求每次读5个，读完继续读
            int length = 0;
            while ((length = fr.read(c)) != -1) {
                msg += new String(c, 0, length);
            }*/
            System.out.println(msg);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
